package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.CheckItem;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author xiaycbstart
 * @create 2019 12 17 20:08
 */
public interface CheckItemDao {
    @Insert("insert into t_checkitem values(null,#{code},#{name},#{sex},#{age},#{price},#{type},#{attention},#{remark})")
    void add(CheckItem checkItem);

    @Select("<script>" +
            "select * from t_checkitem\n" +
            "<if test=\"value != null and value.length > 0\">\n" +
            "where code like '%' #{value} '%' or name like '%' #{value} '%'\n" +
            "</if>" +
            "</script>")
        //模糊查询  '%${value}%' 使用$获取
    Page<CheckItem> pageQuery(String value);

    @Delete("delete from t_checkitem where id = #{id}")
    void delete(Integer id);

    @Select("select count(*) from t_checkgroup_checkitem where checkitem_id =#{id} ")
    long findCountByCheckItemId(Integer id);

    @Update("<script> " +
            "       update t_checkitem\n" +
            "        <set>\n" +
            "            <if test=\"name != null\">\n" +
            "                name = #{name},\n" +
            "            </if>\n" +
            "            <if test=\"sex != null\">\n" +
            "                sex = #{sex},\n" +
            "            </if>\n" +
            "            <if test=\"code != null\">\n" +
            "                code = #{code},\n" +
            "            </if>\n" +
            "            <if test=\"age != null\">\n" +
            "                age = #{age},\n" +
            "            </if>\n" +
            "            <if test=\"price != null\">\n" +
            "                price = #{price},\n" +
            "            </if>\n" +
            "            <if test=\"type != null\">\n" +
            "                type = #{type},\n" +
            "            </if>\n" +
            "            <if test=\"attention != null\">\n" +
            "                attention = #{attention},\n" +
            "            </if>\n" +
            "            <if test=\"remark != null\">\n" +
            "                remark = #{remark},\n" +
            "            </if>\n" +
            "        </set>\n" +
            "          where id = #{id}\n" +
            "</script>")
    void update(CheckItem checkItem);

    @Select("select * from t_checkitem")
    List<CheckItem> findAll();

    @Select("select t1.* from t_checkitem t1,t_checkgroup_checkitem t2 where t2.checkgroup_id = #{id} and t2.checkitem_id = t1.id")
    List<CheckItem> findByCheckgroupId(Integer id);

    @Select("select * from t_checkitem where id  = #{id}")
    CheckItem findById(Integer id);
}
